package com.sy2.netshop.controller;

import com.sy2.netshop.core.Result;
import com.sy2.netshop.entity.Commodity;
import com.sy2.netshop.service.ComService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class CommodityController {
    @Autowired
    private ComService comService;

    @GetMapping("/index")
    public String init(Model model){
        Result result = comService.searchCommodity("", 0);
        List<Commodity> data = (List<Commodity>) result.getData();
        model.addAttribute("commoditys",data);
        return "index";
    }
    @RequestMapping("/search")
    public String search(Model model, Commodity commodity){
        String pname = commodity.getPname();
        Result result = comService.searchCommodity(pname, 0);
        List<Commodity> commodities = (List<Commodity>) result.getData();
        model.addAttribute("commoditys",commodities);
        return "index";
    }
}
